package com.wangsd.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.shop.entity.Customer;
import com.wangsd.web.shop.service.CustomerService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.entity.UploadFile;
import com.wangsd.web.sys.service.UploadFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "客户(商圈)管理")
@RestController
@RequestMapping("/shop/customer")
@Slf4j
public class CustomerController extends MyController {
    @Resource
    private CustomerService service;
    @Autowired
    private UploadFileService uploadFileService;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Customer queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Customer> list = service.selectPage(queryObj, loginUser.getParentCode());
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody Customer entity) {
//        if (entity.getGridList() != null) {
//            //String grids = Arrays.toString(entity.getGridList().toArray());
//            String grids = entity.getGridList().stream().map(i -> i + "").collect(Collectors.joining(","));
//            entity.setGrids(grids);
//        }
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除客户(商圈)")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        service.deleteById(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有客户（商圈）（下拉列表）")
    @PostMapping("/selectAllCustomer")
    public Result selectAllCustomer() {
        SysUser loginUser = getUserInfo();
        List<Customer> list = service.selectAllCustomer(loginUser.getParentCode());
        return Result.success(list);
    }

    @ApiOperation(value = "查询所有九宫格")
    @GetMapping("/selectGrids")
    public Result selectGrids() {
        UploadFile query = new UploadFile();
        query.setMasterType(5);
        List<UploadFile> list = uploadFileService.selectPage(query, null, null);
        return Result.success(list);
    }
}
